/**
 * @author Tobias Boehm
 *
 * 28.04.2011
 */
package org.anddev.andengine.extension.tbsengine.map;

import org.xml.sax.Attributes;

import android.graphics.Point;

/**
 * This is an abstract representation of a map overview. A map overview
 * object is lightweight in comparison to a real {@link AbsMap} instance, 
 * because it only contains meta information like id, name, player count and size.
 * <p>
 * You should create instances of your implementation of this
 * class by an instance of your implementation of {@link AbsMapOverviewLoader}
 * to benefit from the caching mechanism inside {@link AbsMapOverviewLoader}.
 * 
 * @author Tobias Boehm
 *
 */
public abstract class AbsMapOverview implements TBSmapConstants {
	
	/**
	 * The map ID.
	 */
	protected String mID;
	
	/**
	 * The name of the map.
	 */
	protected String mName;
	
	/**
	 * The player count is related to the count of 
	 * {@link BaseMapObjectPropertyValue#startingpoint}s.
	 */
	protected int mPlayerCount;
	
	/**
	 * The map size in tiles.
	 */
	protected Point mSize;
	
	/**
	 * The path to the related map file.
	 */
	protected String mFilePath;
	
	/**
	 * The version number of the map.
	 */
	protected int mVersion;
	
	
	/**
	 * This is the default constructor. It creates an empty map overview object.
	 */
	public AbsMapOverview(){
		
		this.mSize = new Point();
		
		this.mPlayerCount = 0;
	}
	
	/**
	 * @return the mMapID
	 */
	public String getMapID() {
		return mID;
	}

	/**
	 * @return the mMapName
	 */
	public String getMapName() {
		return mName;
	}
		
	/**
	 * @return the mPlayerCount
	 */
	public int getPlayerCount() {
		return mPlayerCount;
	}
	/**
	 * @param mPlayerCount the mPlayerCount to set
	 */
	public void setPlayerCount(final int pPlayerCount) {
		this.mPlayerCount = pPlayerCount;
	}
	/**
	 * @return the mMapSize
	 */
	public Point getMapSize() {
		return mSize;
	}
	/**
	 * @param mSize the mMapSize to set
	 */
	public void setMapSize(final Point pMapSize) {
		this.mSize = pMapSize;
	}
	/**
	 * @param mFilePath the mMapFilePath to set
	 */
	public void setMapFilePath(final String pMapFilePath) {
		this.mFilePath = pMapFilePath;
	}
	/**
	 * @return the mMapFilePath
	 */
	public String getMapFilePath() {
		return mFilePath;
	}

	
	/**
	 * @return the mVersion
	 */
	public int getMapVersion() {
		return mVersion;
	}
		
	/**
	 * This method filters map properties based on the properties defined inside
	 * {@link TBSmapConstants}.
	 * <p>
	 * It also passes the given attribute object to the {@link #addAdditionalTMXTiledMapProperty(String, String)}
	 * method where you can filter additional map properties.
	 * 
	 * @param pMapProperty
	 */
	public void addTMXTiledMapProperty(final Attributes pMapProperty){
		
		final String propertyName = pMapProperty.getValue("", "name");
		final String propertyValue = pMapProperty.getValue("", "value");
		
		if((propertyName != null) && (propertyValue != null)){
			
			try {
				
				final BaseMapProperty property = BaseMapProperty.valueOf(propertyName);
				
				if(property != null){
					
					switch(property){
					
						case id:
							this.mID = propertyValue;
							break;
													
						case name:
							this.mName = propertyValue;
							break;
										
						case version:
							this.mVersion = new Integer(propertyValue);
							break;
							
						case width:
							this.mSize.x = new Integer(propertyValue);
							break;
							
						case height:
							this.mSize.y = new Integer(propertyValue);
							break;
					}
				}
			
			/*
			 * This exception is expected and will be ignored.
			 */
			} catch (IllegalArgumentException e) {};
			
			this.addAdditionalTMXTiledMapProperty(propertyName, propertyValue);
		}
	}
	
	/**
	 * In this method you can filter additional map properties which are not
	 * part of {@link TBSmapConstants}.
	 * 	 
	 * @param pPropertyName - The name of the property.
	 * @param pPropertyValue - The value of the property.
	 */
	protected abstract void addAdditionalTMXTiledMapProperty(final String pPropertyName, final String pPropertyValue);
	
	@Override
	public String toString() {
		
		return this.mName;
	}
}
